package com.ggjj.morethreadtransactioninsert.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ggjj.morethreadtransactioninsert.entities.TestPo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface TestMapper {

    void _batchInsert(@Param("tableName") String tableName, @Param("list") List<TestPo> list);

    default void batchInsert(String table,List<TestPo> list){
        //每次插1000条
        int batchSize=1000;
        //集合数
        int testPoSize = list.size();
        //循环次数
        int circleCount = testPoSize % batchSize == 0 ? testPoSize / batchSize : testPoSize / batchSize + 1;
        //分批次插入
        for (int i = 0; i < circleCount; i++) {
            int fromIndex = i * batchSize;
            int toIndex = fromIndex + batchSize;
            //有可能toIndex超过集合数因为不是插入整1000的
            if (toIndex > testPoSize){
                toIndex = testPoSize;
            }
            List<TestPo> testPoList = list.subList(fromIndex, toIndex);
            _batchInsert(table,testPoList);
        }

    }

    @Delete("delete from ${tableName}")
    void delete(@Param("tableName") String tableName);
}
